package com.it.admin.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.it.admin.model.vo.dashboard.FindDashboardPVStatisticsInfoRspVO;
import com.it.admin.model.vo.dashboard.FindDashboardStatisticsInfoRspVO;
import com.it.admin.service.AdminDashboardService;
import com.it.common.constant.Constants;
import com.it.common.domain.dos.ArticleDO;
import com.it.common.domain.dos.ArticlePublishCountDO;
import com.it.common.domain.dos.StatisticsArticlePVDO;
import com.it.common.mapper.ArticleMapper;
import com.it.common.mapper.CategoryMapper;
import com.it.common.mapper.StatisticsArticlePVMapper;
import com.it.common.mapper.TagMapper;
import com.it.common.utils.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AdminDashboardServiceImpl implements AdminDashboardService {


    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private TagMapper tagMapper;

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private StatisticsArticlePVMapper articlePVMapper;

    @Override
    public Response findDashboardStatistics() {
        //查询文章总数
        Long articleTotalCount = articleMapper.selectArticleTotal();
        //查询标签总数
        Long tagTotalCount = tagMapper.selectTagTotal();
        //查询分类总数
        Long categoryTotalCount = categoryMapper.selectCategoryTotal();
        // 总浏览量
        List<ArticleDO> articleDOList = articleMapper.selectAllReadNum();
        Long pvTotalCount = 0L;
        if(!CollectionUtils.isEmpty(articleDOList)){
            for(ArticleDO articleDO:articleDOList){
                pvTotalCount += articleDO.getReadNum();
            }
        }
        // 组装 VO 类
        FindDashboardStatisticsInfoRspVO vo = FindDashboardStatisticsInfoRspVO.builder()
                .pvTotalCount(pvTotalCount).articleTotalCount(articleTotalCount)
                .tagTotalCount(tagTotalCount).categoryTotalCount(categoryTotalCount).build();
        return Response.success(vo);
    }

    @Override
    public Response findDashboardPublishArticleStatistics() {
        // 当前日期
        LocalDate currDate = LocalDate.now();

        // 当前日期倒退一年的日期
        LocalDate startDate = currDate.minusYears(1);

        // 查找这一年内，每日发布的文章数量
        List<ArticlePublishCountDO> articlePublishCountDOS = articleMapper.selectDateArticlePublishCount(startDate,currDate.plusDays(1));
        Map<LocalDate,Long> map = new HashMap<>();
        if(!CollectionUtils.isEmpty(articlePublishCountDOS)){
            // DO 转 Map
            Map<LocalDate, Long> dateArticleCountMap = articlePublishCountDOS.stream()
                    .collect(Collectors.toMap(articlePublishCountDO -> articlePublishCountDO.getDate(),
                            articlePublishCountDO -> articlePublishCountDO.getCount()));

            // 有序 Map, 返回的日期文章数需要以升序排列
            map = Maps.newLinkedHashMap();
            // 从上一年的今天循环到今天
            for (; startDate.isBefore(currDate) || startDate.isEqual(currDate); startDate = startDate.plusDays(1)) {
                // 以日期作为 key 从 dateArticleCountMap 中取文章发布总量
                Long count = dateArticleCountMap.get(startDate);
                // 设置到返参 Map
                map.put(startDate, Objects.isNull(count) ? 0 : count);
            }
        }
        return Response.success(map);
    }

    @Override
    public Response findDashboardPVStatistics() {
        // 查询最近一周的 PV 访问量记录
        List<StatisticsArticlePVDO> statisticsArticlePVDOS = articlePVMapper.selectLatestWeekRecords();
        Map<LocalDate,Long> pvDateCountMap = new HashMap<>();
        if(!CollectionUtils.isEmpty(statisticsArticlePVDOS)){
            pvDateCountMap = statisticsArticlePVDOS.stream()
                    .collect(Collectors.toMap(StatisticsArticlePVDO::getPvDate,StatisticsArticlePVDO::getPvCount));

        }
        FindDashboardPVStatisticsInfoRspVO vo = new FindDashboardPVStatisticsInfoRspVO();
        // 日期集合
        List<String> pvDates = Lists.newArrayList();
        // PV 集合
        List<Long> pvCounts = Lists.newArrayList();
        // 当前日期
        LocalDate currentDate = LocalDate.now();
        // 一周前
        LocalDate tmpDate = currentDate.minusWeeks(1);
        // 从一周前开始循环
        System.out.println("=-=-=-=--="+000);
        for(;tmpDate.isEqual(currentDate) || tmpDate.isBefore(currentDate);tmpDate = tmpDate.plusDays(1)){
            pvDates.add(tmpDate.format(Constants.MONTH_DAY_FORMATTER));
            Long count = pvDateCountMap.get(tmpDate);
            pvCounts.add(Objects.isNull(count) ? 0 : count);
        }
        vo.setPvCounts(pvCounts);
        vo.setPvDates(pvDates);
        return Response.success(vo);
    }
}
